package com.fox.contrller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.fox.domain.ResponseResult;
import com.fox.domain.entity.Category;
import com.fox.domain.vo.CategoryVo;
import com.fox.domain.vo.ExcelCategoryVo;
import com.fox.domain.vo.PageVo;
import com.fox.enums.AppHttpCodeEnum;
import com.fox.service.CategoryService;
import com.fox.utils.BeanCopyUtils;
import com.fox.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/listAllCategory")
    public ResponseResult getAllCategory() {
        List<CategoryVo> list = categoryService.getAllCategoryInAdmin();
        return ResponseResult.okResult(list);
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应Json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum,Integer pageSize,Category category) {
        PageVo pageVo = categoryService.selectCategoryPage(category,pageSize,pageNum);
        return ResponseResult.okResult(pageVo);
    }



    /**
     * 更新分类
     */
    @PutMapping
    public ResponseResult edit(@RequestBody Category category){
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    @GetMapping(value = "/{id}")
    public ResponseResult getInfo(@PathVariable(value = "id")Long id){
        Category category = categoryService.getById(id);
        return ResponseResult.okResult(category);
    }

    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable Long id) {
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }

    @PostMapping
    public ResponseResult add(@RequestBody Category category){
        categoryService.save(category);
        return ResponseResult.okResult();
    }




}
